﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace NutWinFrm.Test
{
    public partial class TestTaskFrm : Form
    {
        public TestTaskFrm()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Task.Factory.StartNew<string>(() => { return "One"; }).ContinueWith(ss => { Console.WriteLine(ss.Result); });


            
        }

        private void btn01_Click(object sender, EventArgs e)
        {
            //Thread t2 = new Thread(new ParameterizedThreadStart(TestMethod));
           
            //t2.IsBackground = true;
            //TestObject to = new TestObject();

            //t2.Start(to);
            TestObject to2 = new TestObject();
            Task<TestObject> t = new Task<TestObject>(TestMethod2,to2);
            
           
            t.Start();
           // t.Wait();
            richTextBox1.Text = t.Result.value;
           
        }
        

        public static void TestMethod(object data)
        {
          
            TestObject datastr = data as TestObject;
            Thread t = datastr.t;
            t.Join(10000);
            datastr.value = "only test 1";
            Console.WriteLine("带参数的线程函数，参数为：{0}", datastr.value);

           
        }
        public static TestObject TestMethod2(object data)
        {
            Thread.Sleep(10000);
            Thread t2 = new Thread(new ParameterizedThreadStart(TestMethod));

            t2.IsBackground = true;
            TestObject to = new TestObject();
            to.t = t2;
            t2.Start(to);
            
           
            
            TestObject datastr = data as TestObject;
            datastr.value = "only test 2";
            Console.WriteLine("带参数的线程函数，参数为：{0}", datastr.value);
            
            
            return datastr;


        }
        public static TestObject TestMethod3(object data)
        {
            Thread t2 = new Thread(new ParameterizedThreadStart(TestMethod));

            t2.IsBackground = true;
            TestObject to = new TestObject();
            to.t = t2;
            t2.Start(to);


            TestObject datastr = data as TestObject;
            datastr.value = "only test 2";
            Console.WriteLine("带参数的线程函数，参数为：{0}", datastr.value);


            return datastr;


        }

        private void button2_Click(object sender, EventArgs e)
        {
            TestObject to = new TestObject();
            ThreadPool.QueueUserWorkItem(TestMethod, to);
        }
    }
}
